﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;

namespace QL_HoSoBA
{
    public partial class frmHeThong : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public frmHeThong()
        {
            InitializeComponent();
        }

        public static HoSoBA_Manager hoso = new HoSoBA_Manager();
        public static string taikhoan = "", matkhau = "", chucvu = "", manv = "", tennv = "", phongkham = "";
        private static PhieuTheoDoi_Manager phieutd = new PhieuTheoDoi_Manager();

        #region Xử Lý Sự Kiện Đăng Nhập
        private void tsmDangNhap_Click(object sender, EventArgs e)
        {
            if (chucvu == "")
            {
                frmLogin frm = new frmLogin();
                frm.MdiParent = this;
                frm.Show();
                tsmDangNhap.Enabled = false;
                frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
                tsmDangNhap.Enabled = true;
            }
            else
                MessageBox.Show("Bạn đang đăng nhập tài khoản " + taikhoan.Trim() + "...!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        public void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (chucvu != "")
            {
                this.labelItemTenTaiKhoan.Text = "------Người dùng: " + tennv.Trim() + " --- Chức vụ: "+ chucvu.Trim() + " đang đăng nhập hệ thống-------";
                this.labelItemTenTaiKhoan.Enabled = true;
                this.tsmDangXuat.Visible = true;               
                HienChucNang(chucvu);
                if (chucvu == "Bác Sĩ")
                {
                    phongkham = hoso.PhongKham_TimTheoMaBS(manv).Rows[0][0].ToString();
                }
                if (chucvu == "Y Tá")
                {
                    phongkham = phieutd.PhongBenh_TimTheoMaYTa(manv).Rows[0][0].ToString();
                }
            }
        }

        public void HienChucNang(string chucnang)
        {
            switch (chucnang)
            {
                case "Trưởng khoa":

                    break;
                case "Phó khoa":

                    break;
                case "Bác Sĩ":
                    tsmQuanLy.Visible = true;
                    tsmKhamBenh.Visible = true;
                    tsmTiepNhanBenhNhan.Visible = false;
                    tsmDieuTriTheoDoi.Visible = false;
                    tsmDangNhap.Visible = false;
                    tsmThongTinTK.Visible = true;
                    //tsmDangKy.Visible = false;
                    tsmCapNhatTaiKhoan.Visible = true;
                    tsmDoiMatKhau.Visible = true;
                    break;
                case "Y Tá":
                    tsmDieuTriTheoDoi.Visible = true;
                    tsmQuanLy.Visible = true;
                    tsmTiepNhanBenhNhan.Visible = false;
                    tsmKhamBenh.Visible = false;
                    tsmDangNhap.Visible = false;
                    tsmThongTinTK.Visible = true;
                    //tsmDangKy.Visible = false;
                    tsmCapNhatTaiKhoan.Visible = true;
                    tsmDoiMatKhau.Visible = true;
                    break;
                case "NV Tiếp Nhận":
                    tsmQuanLy.Visible = true;
                    tsmTiepNhanBenhNhan.Visible = true;
                    tsmDieuTriTheoDoi.Visible = false;
                    tsmKhamBenh.Visible = false;
                    tsmDangNhap.Visible = false;
                    tsmThongTinTK.Visible = true;
                    //tsmDangKy.Visible = false;
                    tsmCapNhatTaiKhoan.Visible = true;
                    tsmDoiMatKhau.Visible = true;
                    break;
                case "ADmin":
                    tsmTiepNhanBenhNhan.Visible = false;
                    tsmKhamBenh.Visible = false;
                    tsmDieuTriTheoDoi.Visible = false;
                    tsmThongTinTK.Visible = true;
                    tsmDangKy.Visible = true;
                    tsmCapNhatTaiKhoan.Visible = true;
                    tsmDoiMatKhau.Visible = true;
                    tsmDangNhap.Visible = false;
                    break;
            }
        }

        #endregion

        #region Xử Lý Sự Kiện Đăng Xuất

        private void tsmDangXuat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ?", "???", MessageBoxButtons.YesNo,
               MessageBoxIcon.Question) == DialogResult.Yes)
            {
                taikhoan = ""; matkhau = ""; chucvu = ""; manv = ""; tennv = "";
                this.labelItemTenTaiKhoan.Text = "";
                this.labelItemTenTaiKhoan.Enabled = false;
                foreach (Form frm in this.MdiChildren)
                {
                    frm.Close();
                }
                HienChucNang(false);
                tsmDangNhap.Visible = true;
            }
        }

        public void HienChucNang(bool flag)
        {
            tsmDangXuat.Visible = flag;       
            tsmTiepNhanBenhNhan.Visible = flag;
            tsmKhamBenh.Visible = flag;
            tsmDieuTriTheoDoi.Visible = flag;
            tsmThongTinTK.Visible = flag;
            tsmDangKy.Visible = flag;
            tsmCapNhatTaiKhoan.Visible = flag;
            tsmDoiMatKhau.Visible = flag;
        }

        #endregion

        private void frmHeThong_Load(object sender, EventArgs e)
        {
            HienChucNang(false);
        }

        private void tsmThoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #region Các hàm chức năng của Nhân Viên Tiếp Nhận

        private void tsmThemBenhNhan_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmNhanVienTiepNhan)
                        return;
                }
                frmNhanVienTiepNhan frm = new frmNhanVienTiepNhan();
                frm.MdiParent = this;
                frm.ShowForm(0);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmXoaBenhNhan_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmNhanVienTiepNhan)
                        return;
                }
                frmNhanVienTiepNhan frm = new frmNhanVienTiepNhan();
                frm.MdiParent = this;
                frm.ShowForm(1);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmTimKiemThongKe_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmNhanVienTiepNhan)
                        return;
                }
                frmNhanVienTiepNhan frm = new frmNhanVienTiepNhan();
                frm.MdiParent = this;
               frm.ShowForm(1);
                frm.Show();
            }
            catch
            { }
        }
        #endregion

        #region Các hàm chức năng của Y Tá

        private void tsmXemThongTinDieuTri_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmYTa)
                        return;
                }
                frmYTa frm = new frmYTa();
                frm.MdiParent = this;
                frm.ShowForm(0);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmGhiPhieuTheoDoi_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmYTa)
                        return;
                }
                frmYTa frm = new frmYTa();
                frm.MdiParent = this;
                frm.ShowForm(1);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmCapNhatPhieuTheoDoi_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmYTa)
                        return;
                }
                frmYTa frm = new frmYTa();
                frm.MdiParent = this;
                frm.ShowForm(1);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmXoaPhieuTheoDoi_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmYTa)
                        return;
                }
                frmYTa frm = new frmYTa();
                frm.MdiParent = this;
                frm.ShowForm(2);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmTimKiemTKPhieuTD_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmYTa)
                        return;
                }
                frmYTa frm = new frmYTa();
                frm.MdiParent = this;
                frm.ShowForm(2);
                frm.Show();
            }
            catch
            { }
        }
        #endregion

        #region Các hàm chức năng của Bác Sĩ

        private void tsmLapBenhAn_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmBacSi)
                        return;
                }
                frmBacSi frm = new frmBacSi();
                frm.MdiParent = this;
                frm.ShowForm(0);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmCapNhatBenhAn_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmBacSi)
                        return;
                }
                frmBacSi frm = new frmBacSi();
                frm.MdiParent = this;
                frm.ShowForm(1);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmXoaHSBenhAn_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmBacSi)
                        return;
                }
                frmBacSi frm = new frmBacSi();
                frm.MdiParent = this;
                frm.ShowForm(2);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmTimVaThongKe_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmBacSi)
                        return;
                }
                frmBacSi frm = new frmBacSi();
                frm.MdiParent = this;
                frm.ShowForm(2);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmHoanTatHS_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmBacSi)
                        return;
                }
                frmBacSi frm = new frmBacSi();
                frm.MdiParent = this;
                frm.ShowForm(5);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmGhiPhieuDT_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmBacSi)
                        return;
                }
                frmBacSi frm = new frmBacSi();
                frm.MdiParent = this;
                frm.ShowForm(3);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmCapNhatPhieuDT_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmBacSi)
                        return;
                }
                frmBacSi frm = new frmBacSi();
                frm.MdiParent = this;
                frm.ShowForm(3);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmXoaPhieuDT_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (Form frm1 in this.MdiChildren)
                {
                    if (frm1 is frmBacSi)
                        return;
                }
                frmBacSi frm = new frmBacSi();
                frm.MdiParent = this;
                frm.ShowForm(4);
                frm.Show();
            }
            catch
            { }
        }

        private void tsmDangKy_Click(object sender, EventArgs e)
        {
            foreach (Form frm1 in this.MdiChildren)
            {
                if (frm1 is frmTaiKhoan)
                    return;
            }
            frmTaiKhoan frm = new frmTaiKhoan();
            frm.MdiParent = this;
            frm.ShowForm(0);
            frm.Show();
        }

        private void tsmCapNhatTaiKhoan_Click(object sender, EventArgs e)
        {
            foreach (Form frm1 in this.MdiChildren)
            {
                if (frm1 is frmTaiKhoan)
                    return;
            }
            frmTaiKhoan frm = new frmTaiKhoan();
            frm.MdiParent = this;
            frm.ShowForm(1);
            frm.Show();
        }

        private void tsmDoiMatKhau_Click(object sender, EventArgs e)
        {
            foreach (Form frm1 in this.MdiChildren)
            {
                if (frm1 is frmTaiKhoan)
                    return;
            }
            frmTaiKhoan frm = new frmTaiKhoan();
            frm.MdiParent = this;
            frm.ShowForm(1);
            frm.Show();
        }

        private void tsmLayLaiMatKhau_Click(object sender, EventArgs e)
        {
            foreach (Form frm1 in this.MdiChildren)
            {
                if (frm1 is frmTaiKhoan)
                    return;
            }
            frmTaiKhoan frm = new frmTaiKhoan();
            frm.MdiParent = this;
            frm.ShowForm(2);
            frm.Show();
        }
        #endregion

        private void tsmThongTinTK_Click(object sender, EventArgs e)
        {
            FrmThongTinTK frm = new FrmThongTinTK();
            frm.MdiParent = this;
            frm.Show();
        }
    }
}
